package cn.stronglink.asset.message.mq.dam;

import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.google.gson.reflect.TypeToken;

import cn.stronglink.asset.message.mq.dam.handle.AssetBindRfidPageHandle;
import cn.stronglink.asset.message.mq.dam.handle.AssetModelBindUHeightHandle;
import cn.stronglink.asset.message.mq.dam.handle.MessangHandleContext;
import cn.stronglink.asset.message.mq.dam.handle.OpenAssetDetailPageHandle;
import cn.stronglink.asset.message.mq.dam.handle.OpenAssetListLastPageHandle;
import cn.stronglink.asset.message.mq.dam.handle.OpenAssetListNextPageHandle;
import cn.stronglink.asset.message.mq.dam.handle.OpenAssetListPageHandle;
import cn.stronglink.asset.message.mq.dam.handle.OpenBindDevicePageHandle;
import cn.stronglink.asset.message.mq.dam.handle.OpenBindRfidPageHandle;
import cn.stronglink.asset.message.mq.dam.handle.OpenMicroenvironmentPageHandle;
import cn.stronglink.asset.message.mq.dam.handle.OpenRackPageHandle;
import cn.stronglink.asset.message.mq.dam.handle.OpenSetModelUHeightPageHandle;
import cn.stronglink.asset.message.mq.dam.handle.RackBindDeviceCodeHandle;
import cn.stronglink.asset.message.mq.dam.message.MQMessageOfDAM;

@Component("ScreenCommandReceiver")
public class ScreenCommandReceiver implements MessageListener {
	protected final Logger logger = LoggerFactory.getLogger(RealtimeMessageReceiver.class);
	protected final MessangHandleContext messageHandle = new MessangHandleContext();

	public ScreenCommandReceiver() {
		super();
		this.messageHandle.addHandle(new OpenRackPageHandle());
		this.messageHandle.addHandle(new OpenAssetListPageHandle());
		this.messageHandle.addHandle(new OpenAssetListLastPageHandle());
		this.messageHandle.addHandle(new OpenAssetListNextPageHandle());
		this.messageHandle.addHandle(new OpenAssetDetailPageHandle());
		this.messageHandle.addHandle(new OpenMicroenvironmentPageHandle());
		this.messageHandle.addHandle(new OpenBindDevicePageHandle());
		this.messageHandle.addHandle(new RackBindDeviceCodeHandle());
		this.messageHandle.addHandle(new OpenBindRfidPageHandle());
		this.messageHandle.addHandle(new AssetBindRfidPageHandle());
		this.messageHandle.addHandle(new OpenSetModelUHeightPageHandle());
		this.messageHandle.addHandle(new AssetModelBindUHeightHandle());
	}
	
	@Override
	public void onMessage(Message message) {
		try {
			if (message instanceof TextMessage) {
				TextMessage tm = (TextMessage) message;
				String json = tm.getText();
				logger.info("接收到MQ数据:{}", json);
				MQMessageOfDAM rm = JSON.parseObject(json, new TypeToken<MQMessageOfDAM>() {
				}.getType());
				this.messageHandle.handle(rm);
			} else {
				logger.error("无法解析的mq对象消息:" + message.getClass().getName());
			}
		} catch (Exception e) {
			logger.error(e.getMessage());
		}
	}
}
